let {
	$U_SetItem,
	$U_GetItem,
	$U_RemoveItem,
} = require('@utils/storage')

module.exports = {

	/**
	 * 添加模块/子模块历史记录
	 * @param {[type]} module [description]
	 */
	addModuleHistory(apiId, moduleId, isChild = false, parentId = '') {
		// return
		let {
			store,
		} = this
		store.history.apis[apiId] = {
			id: apiId,
			operate: 'addApi',
			moduleId,
		}
		store.history.modules[moduleId] = {
			id: moduleId,
			operate: isChild ? 'addChildModule' : 'addModule',
			parentId,
		}

		saveTmpDatas(this, apiId)
	},

	/**
	 * 添加api历史记录
	 * @param {[type]} api [description]
	 */
	addApiHistory(apiId, moduleId) {
		// return
		let {
			store,
		} = this
		store.history.apis[apiId] = {
			id: apiId,
			operate: 'addApi',
			moduleId,
		}

		saveTmpDatas(this, apiId)
	},

	/**
	 * 删除模块/api节点历史记录
	 * @param  {[type]} node [description]
	 * @param  {[type]} type [description]
	 * @return {[type]}      [description]
	 */
	delNodeHistory(delInfo) {
		// return
		let {
			store,
		} = this;
		let histInfo = store.history[delInfo.type][delInfo.id]
		if (histInfo && histInfo.operate && histInfo.operate.indexOf('add') > -1) {
			delete store.history[delInfo.type][delInfo.id]
			return false
		}
		store.history[delInfo.type][delInfo.id] = {
			id: delInfo.id,
			operate: delInfo.type == 'apis' ? 'delApi' : 'delModule',
			moduleId: delInfo.moduleId
		}

		saveTmpDatas(this)
	},

	/**
	 * 模块/接口名修改记录
	 * @param  {[type]} type    [description]
	 * @param  {[type]} oldName [description]
	 * @param  {[type]} newName [description]
	 * @return {[type]}         [description]
	 */
	nameModifyHistory(type, nodeId, newValue, moduleId) {
		// return
		let {
			store,
		} = this
		if (type == 'modules') {
			store.history.modules[nodeId] = store.history.modules[nodeId] || {
				id: nodeId,
				operate: 'nameModify',
			}
		} else {
			let api = checkApi(store, moduleId)
			if (!api)
				return false
			api.operateAttributes.name = newValue
		}

		saveTmpDatas(this)
	},

	/**
	 * 接口详情修改历史记录
	 * @param  {[type]} detail    [description]
	 * @param  {[type]} attribute [description]
	 * @param  {[type]} newValue  [description]
	 * @param  {[type]} oldValue  [description]
	 * @return {[type]}           [description]
	 */
	apiDetailModifyHistory(attribute, newValue, oldValue, moduleId) {
		// return
		let {
			store,
		} = this
		let api = checkApi(store, moduleId)
		if (!api)
			return false
		api.operateAttributes[attribute] = newValue

		saveTmpDatas(this)
	},

	/**
	 * 接口参数历史记录
	 * @param  {[type]} flag      [description]
	 * @param  {[type]} type      [description]
	 * @param  {[type]} oldValues [description]
	 * @param  {[type]} newValues [description]
	 * @return {[type]}           [description]
	 */
	apiParameterHistory(flag, type, oldValues, newValues, moduleId) {
		// return
		let {
			store,
		} = this
		if (type == 'modify') {
			let diff = false
			let keyArr = Object.keys(oldValues)
			for (let i = 0; i < keyArr.length; i++) {
				let key = keyArr[i]
				if (oldValues[key] != newValues[key]) {
					diff = true
					break
				}
			}
			if (!diff)
				return false
		}
		let api = checkApi(store, moduleId)
		if (!api)
			return false
		let _treeId_ = oldValues._treeId_
		let parameter = api.operateParameters[_treeId_] = api.operateParameters[_treeId_] || {
			_treeId_,
			name: {
				oldValue: oldValues.name,
				newValue: oldValues.name,
			},
			type: {
				oldValue: oldValues.type,
				newValue: oldValues.type,
			},
			content: {
				oldValue: oldValues.content,
				newValue: oldValues.content,
			},
			remark: {
				oldValue: oldValues.remark,
				newValue: oldValues.remark,
			},
			mandatory: {
				oldValue: oldValues.mandatory,
				newValue: oldValues.mandatory,
			},
		}
		if (type == 'modify') {
			parameter.name.newValue = newValues.name
			parameter.type.newValue = newValues.type
			parameter.content.newValue = newValues.content
			parameter.remark.newValue = newValues.remark
			parameter.mandatory.newValue = newValues.mandatory
		}
		if (type == 'del' && parameter.operate == 'add')
			delete api.operateParameters[_treeId_]
		if (parameter.operate != 'add') {
			parameter.operate = type
			parameter.flag = flag
		}
		saveTmpDatas(this, api.id)
	},

	/**
	 * 导入JSON数据历史
	 * @param  {[type]} JSONData [description]
	 * @return {[type]}          [description]
	 */
	importJSONHistory(JSONData, moduleId) {
		// return
		let {
			store,
		} = this
		let api = checkApi(store, moduleId)
		if (!api)
			return false
		api.operateJSON = $.extend((api.operateJSON || {}), JSONData)

		saveTmpDatas(this)
	},

	/**
	 * 导入模板数据
	 * @param  {[type]} template [description]
	 * @return {[type]}          [description]
	 */
	importTemplateHistory(template, moduleId) {
		// return
		let {
			store,
		} = this
		let api = checkApi(store, moduleId)
		if (!api)
			return false
		api.operateTemplate = template

		saveTmpDatas(this)
	},

	/**
	 * 保存时和上一个版本比较差异
	 * @return {[type]} [description]
	 */
	diffHistory(store) {
		// return
		let {
			orgProjectData,
			editProjectData,
			history,
		} = store
		let historyHTML = '';
		// 历史记录
		let apiKeyArr = Object.keys(history.apis);
		let moduleKeyArr = Object.keys(history.modules);
		// 当前最终数据
		let editApis = editProjectData.apis;
		let editModules = editProjectData.modules;;
		// 原始数据
		let orgApis = orgProjectData.apis;
		let orgModules = orgProjectData.modules;

		console.log('editProjectData', editProjectData)
		console.log('orgProjectData', orgProjectData)
		console.log('history', history)

		// 记录图标
		// edit_add	添加
		// pencil	修改
		// cancel	删除
		let operateImg = '<span style="padding: 2px 10px;"><img src="/easyui/themes/icons/#icon.png"></span>';
		// 模块修改对比
		for (let i = 0; i < moduleKeyArr.length; i++) {
			let pHTML = '<p>'
			let moduleId = moduleKeyArr[i]
			let moduleHistory = history.modules[moduleId]
			let edit = editModules[moduleId]
			let org = orgModules[moduleId]
			if (moduleHistory.operate == 'addModule') {
				// 添加模块
				pHTML += operateImg.replace('#icon', 'edit_add')
				pHTML += '<span>添加模块: </span>'
				pHTML += `[<span style="font-weight:bold;color:#EC971F;">${edit.name}</span>]`
			}
			if (moduleHistory.operate == 'addChildModule') {
				// 添加子模块
				pHTML += operateImg.replace('#icon', 'edit_add')
				pHTML += '<span>添加子模块: </span>'
				pHTML += `[<span style="font-weight:bold;color:#EC971F;">${edit.name}</span>]`
			}
			if (moduleHistory.operate == 'delModule') {
				// 删除模块
				pHTML += operateImg.replace('#icon', 'cancel')
				pHTML += '<span>删除模块: </span>'
				pHTML += `[<span style="font-weight:bold;color:#EC971F;">${org.name}</span>]`
			}
			if (moduleHistory.operate == 'nameModify') {
				// 模块名称修改
				pHTML += operateImg.replace('#icon', 'pencil')
				pHTML += '<span>模块名修改: </span>'
				pHTML += `[<span style="font-weight:bold;color:#b5b0b0;">${org.name}</span> ===>>> `
				pHTML += `<span style="font-weight:bold;color:#C9302C;">${edit.name}</span>]`
			}
			pHTML += '</p>'
			historyHTML += pHTML
		}

		// 接口修改对比
		for (let i = 0; i < apiKeyArr.length; i++) {
			let pHTML = '<p>'
			let apiId = apiKeyArr[i]
			let apiHistory = history.apis[apiId]
			let edit = editApis[apiId]
			let org = orgApis[apiId]
			let moduleId = apiHistory.moduleId || ''
			let module = editModules[moduleId] || orgModules[moduleId]
			if (apiHistory.operate == 'addApi') {
				// 添加接口
				pHTML += operateImg.replace('#icon', 'edit_add')
				pHTML += '<span>添加接口: </span>'
				pHTML += `[<span style="font-weight:bold;color:#50cd28;">${edit.name}</span>]`
					// if (module && module.name)
					// 	pHTML += `所属模块: [<span style="font-weight:bold;color:#EC971F;">${module.name}</span>]`
			}
			if (apiHistory.operate == 'delApi') {
				// 删除接口
				pHTML += operateImg.replace('#icon', 'cancel')
				pHTML += '<span>删除接口: </span>'
				pHTML += `[<span style="font-weight:bold;color:#50cd28;">${org.name}</span>]`
					// if (module && module.name)
					// 	pHTML += `<span>所属模块: ${module.name}</span>`
			}
			if (apiHistory.operate == 'apiModify') {
				// 接口内容修改
				pHTML += operateImg.replace('#icon', 'pencil')
				pHTML += `接口: [<span style="font-weight:bold;color:#50cd28;">${edit.name}</span>] 修改`
				pHTML += '<ul>'
				pHTML += apiModifyDiff(apiHistory, module, edit, org);
				pHTML += '</ul>';
			}
			if (module && module.name)
				pHTML += `<span style="margin-left:20px;">所属模块: </span>[<span style="font-weight:bold;color:#EC971F;">${module.name}</span>]`
			pHTML += '</p>'
			historyHTML += pHTML
		}
		if (historyHTML)
			historyHTML = `<div style="font-size: 18px;">${historyHTML}</div>`
		return historyHTML
	},
}

/**
 * 检查该接口是否为新建接口接口
 * 新建接口不需要记录详情
 * @param  {[type]}  store     [description]
 * @return {Boolean}           [description]
 */
function checkApi(store, moduleId) {
	let apiDetail = store._currentEditApiDetail
	if (!apiDetail || !apiDetail.id)
		return false
	let apis = store.history.apis
	let apiId = apiDetail.id
	let apiHistory = apis[apiId] || null
	if (apiHistory && apiHistory.operate.indexOf('add') > -1)
		return false
	apis[apiId] = apis[apiId] || {
		id: apiId,
		operate: 'apiModify',
		operateAttributes: {},
		operateParameters: {},
		operateJSON: null,
		operateTemplate: '',
		moduleId,
	};
	return apis[apiId]
}

function apiModifyDiff(apiHistory, module, edit, org) {
	let html = ''
	if (apiHistory.operateTemplate) {
		html += `<li>导入模板[<span>${apiHistory.operateTemplate}</span>]</li>`
	}
	if (apiHistory.operateJSON) {
		html += `<li><span>导入JSON数据</span></li>`
	}
	// 接口详情参数修改
	let operateAttributes = apiHistory.operateAttributes
	if (operateAttributes.name && edit.name != org.name) {
		// 接口名修改
		// style="font-weight:bold;color:#b5b0b0;"
		// style="font-weight:bold;color:#C9302C;"
		html += '<li>'
		html += '<span>接口名</span>修改: '
		html += `[<span style="font-weight:bold;color:#b5b0b0;">${org.name}</span> ===>>> `
		html += `<span style="font-weight:bold;color:#C9302C;">${edit.name}</span>]`
		html += '</li>'
	}
	if (operateAttributes.intro && edit.intro != org.intro) {
		html += '<li>'
		html += '<span>接口描述</span>修改:'
		html += `<span style="font-weight:bold;color:#b5b0b0;">${org.intro}</span> ===>>> `
		html += `<span style="font-weight:bold;color:#C9302C;">${edit.intro}</span>`
		html += '</li>'
	}
	if (operateAttributes.method && edit.method != org.method) {
		html += '<li>'
		html += '<span>请求方法</span>修改:'
		html += `[<span style="font-weight:bold;color:#b5b0b0;">${org.method}</span> ===>>> `
		html += `<span style="font-weight:bold;color:#C9302C;">${edit.method}</span>]`
		html += '</li>'
	}
	if (operateAttributes.path && edit.path != org.path) {
		html += '<li>'
		html += '<span>请求路径</span>修改:'
		html += `[<span style="font-weight:bold;color:#b5b0b0;">${org.path}</span> ===>>> `
		html += `<span style="font-weight:bold;color:#C9302C;">${edit.path}</span>]`
		html += '</li>'
	}
	if (operateAttributes.contentType && edit.contentType != org.contentType) {
		html += '<li>'
		html += '<span>请求类型</span>修改:'
		html += `[<span style="font-weight:bold;color:#b5b0b0;">${org.contentType}</span> ===>>> `
		html += `<span style="font-weight:bold;color:#C9302C;">${edit.contentType}</span>]`
		html += '</li>'
	}
	// 接口参数修改
	let operateParameters = apiHistory.operateParameters
	let paramValArr = Object.values(operateParameters)
	for (let i = 0; i < paramValArr.length; i++) {
		let {
			content,
			flag,
			mandatory,
			name,
			operate,
			remark,
			type,
		} = paramValArr[i]
		flag = flag == 'req' ? '请求' : '响应'
		if (operate == 'add') {
			html += '<li>'
			html += `<span>添加[${flag}]参数: </span>`
			html += `<span>${name.newValue}</span>`
			html += '</li>'
		}
		if (operate == 'del') {
			html += '<li>'
			html += `<span>删除[${flag}]参数: </span>`
			html += `<span>${name.newValue}</span>`
			html += '</li>'
		}
		if (operate == 'modify') {
			// 参数属性变更
			if (name.oldValue != name.newValue) {
				html += '<li>'
				html += `<span>[${flag}]参数</span>`
				html += '<span style="font-weight:bold;color:#c71bdc;">名</span>修改'
				html += `[<span style="font-weight:bold;color:#b5b0b0;">${name.oldValue}</span> ===>>> `
				html += `<span style="font-weight:bold;color:#C9302C;">${name.newValue}</span>]`
				html += '</li>'
			}
			if (mandatory.oldValue != mandatory.newValue) {
				html += '<li>'
				html += `<span>[${flag}]参数</span>`
				html += '<span style="font-weight:bold;color:#c71bdc;">可选</span>变更'
				html += `[<span style="font-weight:bold;color:#b5b0b0;">${mandatory.oldValue}</span> ===>>> `
				html += `<span style="font-weight:bold;color:#C9302C;">${mandatory.newValue}</span>]`
				html += '</li>'
			}
			if (type.oldValue != type.newValue) {
				html += '<li>'
				html += `<span>[${flag}]参数</span>`
				html += '<span style="font-weight:bold;color:#c71bdc;">类型</span>变更'
				html += `[<span style="font-weight:bold;color:#b5b0b0;">${type.oldValue}</span> ===>>> `
				html += `<span style="font-weight:bold;color:#C9302C;">${type.newValue}</span>]`
				html += '</li>'
			}
			if (content.oldValue != content.newValue) {
				html += '<li>'
				html += `<span>[${flag}]参数</span>`
				html += '<span style="font-weight:bold;color:#c71bdc;">含义</span>变更'
				html += `[<span style="font-weight:bold;color:#b5b0b0;">${content.oldValue}</span> ===>>> `
				html += `<span style="font-weight:bold;color:#C9302C;">${content.newValue}</span>]`
				html += '</li>'
			}
			if (remark.oldValue != remark.newValue) {
				html += '<li>'
				html += `<span>[${flag}]参数</span>`
				html += '<span style="font-weight:bold;color:#c71bdc;">备注</span>变更'
				html += `[<span style="font-weight:bold;color:#b5b0b0;">${remark.oldValue}</span> ===>>> `
				html += `<span style="font-weight:bold;color:#C9302C;">${remark.newValue}</span>]`
				html += '</li>'
			}
		}
	}
	return html
}

/**
 * 缓存临时数据,处理非正常退出情况,
 * 下次进入可继续编辑
 * @param  {[type]} store [description]
 * @return {[type]}       [description]
 */
function saveTmpDatas(apiComp, apiId) {
	let {
		store,
		reqParamsComp,
		resParamsComp,
	} = apiComp
	let apiDetail = store.editProjectData.apis[apiId];
	if (apiDetail && apiDetail.id) {
		apiDetail.requestParameterList = reqParamsComp.treegrid('getData');
		apiDetail.responseParameterList = resParamsComp.treegrid('getData');
	}
	$U_SetItem('tmp_edit_project_data', store.editProjectData)
	$U_SetItem('tmp_history', store.history)
	$U_SetItem('tmp_current_check_api', store._currentCheckApiDetail)
}